package search;

import java.util.ArrayList;
import java.util.List;

public class BinarySearch {

    public static void main(String[] args) {

    }

    /**
     * 查找单个相同的值
     * @param arr
     * @param left
     * @param right
     * @param findVal
     * @return
     */
    public static int binarySearch(int[] arr, int left, int right, int findVal) {
        if (left > right) {
            return -1;//指的索引
        }
        int mid = (left + right) / 2;
        if (findVal > arr[mid]) {
            return binarySearch(arr, mid + 1, right, findVal);
        } else if (findVal < arr[mid]) {
            return binarySearch(arr, left, mid - 1, findVal);
        } else {
            return mid;
        }
    }

    /**
     * 查找所有相同的值
     * @param arr
     * @param left
     * @param right
     * @param findVal
     * @return
     */
    public static List<Integer> binarySearchPlus(int[] arr, int left, int right, int findVal) {
        List<Integer> list = new ArrayList();
        if (left > right) {
            list.add(-1);
            return list;//指的索引
        }
        int mid = (left + right) / 2;
        if (findVal > arr[mid]) {
            return binarySearchPlus(arr, mid + 1, right, findVal);
        } else if (findVal < arr[mid]) {
            return binarySearchPlus(arr, left, mid - 1, findVal);
        } else {
            return InsertValueSearch.findOtherSame(mid, list, findVal, arr);
        }
    }

}
